﻿using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools.Extension;
using Microsoft.Win32;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Apiservice.InPut;
using NPOI.SS.Formula.Functions;
using Prism.Mvvm;
using Prism.Regions;
using Quartz;
using Quartz.Impl;
using Reconstruct_Desktop.Apiservice;
using Reconstruct_Desktop.Apiservice.Dto;
using Reconstruct_Desktop.Apiservice.InPut;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Reconstruct_Desktop.ViewModels.OrderManageViewModel
{
    public partial class PollingViewModel : BindableBase, INavigationAware
    {




        private readonly IViewService_Client viewService;
        private readonly IHangFireService_Client hangFireService;
        private readonly IBaseService_Client baseService;
        private DispatcherTimer timer;
        public PollingViewModel()
        {
            viewService=new ViewService_Client();
            hangFireService = new HangFireService_Client();
            baseService=new BaseService_Client();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1); // 设置计时器的时间间隔为1秒
            timer.Tick += Timer_Tick; // 订阅计时器的Tick事件                             
            timer.Start(); // 启动计时器
            InPut_ViewService5 = new InPut_ViewService5_Client();
            InPut_ViewService12=new InPut_ViewService12_Client();
            InPut_ViewService10 = new InPut_ViewService10_Client();
            InPut_ViewService13=new InPut_ViewService13_Client();
            View_TaskOrderDto = new List<View_TaskOrderDto>();
            PutOutOrderDto = new List<PutOutOrderDto>();
            View_NeetOutMaterialDto = new ObservableCollection<View_NeetOutMaterialDto>();
      
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler().Result;
            View_TaskOrder();
            View_PutOutOrder();
            View_NeetOutMaterial();
            GetConveyorline();
        }



        private InPut_ViewService5_Client inPut_ViewService5;
        public InPut_ViewService5_Client InPut_ViewService5
        {
            get { return inPut_ViewService5; }
            set { inPut_ViewService5 = value;RaisePropertyChanged(); }
        }

        private InPut_ViewService12_Client inPut_ViewService12;
        public InPut_ViewService12_Client InPut_ViewService12
        {
            get { return inPut_ViewService12; }
            set { inPut_ViewService12 = value; RaisePropertyChanged(); }
        }
        private InPut_ViewService10_Client inPut_ViewService10;
        public InPut_ViewService10_Client InPut_ViewService10
        {
            get { return inPut_ViewService10; }
            set { inPut_ViewService10 = value; RaisePropertyChanged(); }
        }

        private List<View_TaskOrderDto> view_TaskOrderDto;
        public List< View_TaskOrderDto> View_TaskOrderDto
        {
            get { return view_TaskOrderDto; }
            set { view_TaskOrderDto = value; RaisePropertyChanged(); }
        }
        private List<PutOutOrderDto> putOutOrderDto;
        public List<PutOutOrderDto> PutOutOrderDto
        {
            get { return putOutOrderDto;  }
            set { putOutOrderDto = value; RaisePropertyChanged(); }
        }
        private ObservableCollection<View_NeetOutMaterialDto> view_NeetOutMaterialDto;
        public ObservableCollection<View_NeetOutMaterialDto> View_NeetOutMaterialDto
        {
            get { return view_NeetOutMaterialDto; }
            set { view_NeetOutMaterialDto = value; RaisePropertyChanged();}
        }

        private InPut_ViewService13_Client inPut_ViewService13;
        public InPut_ViewService13_Client InPut_ViewService13
        {
            get { return inPut_ViewService13; }
            set { inPut_ViewService13 = value;RaisePropertyChanged(); }
        }


        private string conveyor;

        public string Conveyor
        {
            get { return conveyor; }
            set { conveyor = value;RaisePropertyChanged(); }
        }


        //打开hangfire
        [RelayCommand]
        private void ShowPolling()
        {

            string url = ConfigurationManager.AppSettings["HangFireUrl"];
            string browser = ConfigurationManager.AppSettings["browser"]; 
            try
            {
                Process.Start(browser, url); 
            }
            catch (Exception ex)
            {
                Growl.Error("浏览器启动失败");
           
            }
        }
        //任务视图
        [RelayCommand]
        private async Task View_TaskOrder()
        {
            InPut_ViewService5.PageIndex = 1;
            InPut_ViewService5.PageSize = 500;
            var Tuple = await viewService.View_TaskOrder(InPut_ViewService5);
            View_TaskOrderDto = Tuple.Item1;
        }
        //出库工单视图
        [RelayCommand]
        private async Task View_PutOutOrder()
        {
 
           InPut_ViewService13.IsFinish = false;
           PutOutOrderDto = await viewService.View_PutOutOrder2(InPut_ViewService13);
           await View_TaskOrder();
            //View_PutOutOrder();
           await View_NeetOutMaterial();
            GetConveyorline();
        }
        //欠料视图
        [RelayCommand]
        private async Task View_NeetOutMaterial()
        {
            InPut_ViewService10.PageIndex = 1;
            InPut_ViewService10.PageSize = 500;
            var Tuple=   await viewService.View_NeetOutMaterial(InPut_ViewService10);
            View_NeetOutMaterialDto=Tuple.Item1;
        }
        //启动轮询
        [RelayCommand]
        private  void OpenPolling()
        {
            hangFireService.OpenPolling();
        }
        //停止轮询
        [RelayCommand]
        private void ClosePolling()
        {
            hangFireService.ClosePolling();
        }
        //单次执行
        [RelayCommand]
        private async void RunOne(Guid id)
        {
            await baseService.SingleOutbount(id);
                    await View_PutOutOrder();
        }
        //计时器 刷新页面
        private void Timer_Tick(object sender, EventArgs e)
        {
        

        }
        //手动完成工单
        [RelayCommand]
        private async void Manual_FinishOrder(Guid id)
        {
            await baseService.Manual_FinishOrder(id);
            await View_PutOutOrder();
        }
  
        private async void GetConveyorline()
        {
            Conveyor= await baseService.GetConveyorline();
        }

        //页面配置
        private string title = "出库轮询";
        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
          

        }


    }
}
